/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Lop;

import javax.swing.JOptionPane;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;

/**
 *
 * @author lhqbao
 */
public class lNEN {
    private String manen;
    private String tennen;
    private String capphat;
    private lDIENTICH dt;
    private lDIACHINB dcnb;
    private lNHOM_NEN refNHOM_NEN;
    
    public lNEN(){}
    public lNEN(String manen, String tennen, lDIENTICH dt, lDIACHINB dcnb, String capphat, lNHOM_NEN refNHOM_NEN)
    {
        this.manen = manen;
        this.tennen = tennen;
        this.dt = dt;
        this.dcnb = dcnb;
        this.capphat = capphat;
        this.refNHOM_NEN = refNHOM_NEN;
    }
    public lNEN(lNHOM_NEN refNHOM_NEN)
    {
        this.refNHOM_NEN = refNHOM_NEN;
    }

    public String getManen() {
        return manen;
    }

    public void setManen(String manen) {
        this.manen = manen;
    }

    public String getTennen() {
        return tennen;
    }

    public void setTennen(String tennen) {
        this.tennen = tennen;
    }

    public lDIENTICH getDt() {
        return dt;
    }

    public void setDt(lDIENTICH dt) {
        this.dt = dt;
    }

    public lDIACHINB getDcnb() {
        return dcnb;
    }

    public void setDcnb(lDIACHINB dcnb) {
        this.dcnb = dcnb;
    }
    
    public lNHOM_NEN getRefNHOM_NEN() {
        return refNHOM_NEN;
    }

    public void setRefNHOM_NEN(lNHOM_NEN refNHOM_NEN) {
        this.refNHOM_NEN = refNHOM_NEN;
    }

    public String getCapphat() {
        return capphat;
    }

    public void setCapphat(String capphat) {
        this.capphat = capphat;
    }
    
    OraclePreparedStatement pst;
    OracleResultSet rs;
       
    public OracleResultSet hienthiNEN(OConnection cnn){
        try{
            
            String sql = "select n.manen,n.tennen, n.dt.chieudai as chieudai,n.dt.chieurong as chieurong,n.dcnb.sonen as sonen,n.dcnb.tenduong as tenduong, n.capphat from nen n where refNHOM_NEN = (select ref(p) from nhom_nen p where p.manhom=?)";
            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql);
             
            pst.setString(1, refNHOM_NEN.getManhom());
            rs = (OracleResultSet) pst.executeQuery();
         }catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
        return rs;    
    } 
    public OracleResultSet getDsNHOM_NEN(OConnection cnn){
        try{
            
            String sql = "Select manhom, tennhom From NHOM_NEN";
            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql, OracleResultSet.TYPE_SCROLL_INSENSITIVE, OracleResultSet.CONCUR_UPDATABLE);
            rs = (OracleResultSet) pst.executeQuery();
         }catch(Exception e){
             
            JOptionPane.showMessageDialog(null,e);
        }
        return rs;    
    }
    
    public OracleResultSet getDS_NHOMNEN(OConnection cnn){
        try{
            
            String sql = "select * from NHOM_NEN where refKHU_TDC = (select ref(p) from KHU_TDC p where p.makhu=?)";

            //String sql = "select * from quanhuyen";
            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql, OracleResultSet.TYPE_SCROLL_INSENSITIVE, OracleResultSet.CONCUR_UPDATABLE);
            pst.setString(1, this.manen);
            rs = (OracleResultSet) pst.executeQuery();
         }catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
        return rs;    
    } 
    public void themNEN(OConnection cnn)
    {
        try{
            String sql = "insert into NEN values(T_NEN(?,?,T_DIENTICH(?,?),T_DIACHINB(?,?),?,(select ref(p) from nhom_nen p where p.manhom=?)))";

            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql);
            pst.setString(1, this.manen);
            pst.setString(2, this.tennen);
            pst.setFloat(3, this.dt.getChieudai());
            pst.setFloat(4, this.dt.getChieurong());
            pst.setString(5, this.dcnb.getSonen());
            pst.setString(6, this.dcnb.getTenduong());
            pst.setString(7, this.capphat);
            pst.setString(8, this.refNHOM_NEN.getManhom());
            pst.executeQuery();
                    
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
    public void suaNEN(OConnection cnn)
    {
        try{
         String sql = "update nen n set n.tennen=?, n.dt.chieudai=?,n.dt.chieurong=?,n.dcnb.sonen=?,n.dcnb.tenduong, n.capphat=?,(select ref(p) from nhom_nen p where p.manhom=?) where n.manen = ?";
            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql);
            pst.setString(1, this.tennen);
            pst.setFloat(2, this.dt.getChieudai());
            pst.setFloat(3, this.dt.getChieurong());
            pst.setString(4, this.dcnb.getSonen());
            pst.setString(5, this.dcnb.getTenduong());
            pst.setString(6, this.capphat);
            pst.setString(7, this.refNHOM_NEN.getManhom());
             pst.setString(8, this.manen);
            pst.executeQuery();

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        } 
    }    
    public void xoaKHU_TDC(OConnection cnn)
    {
        
        try{
            String sql = "delete from nen where manen=?";
            pst=(OraclePreparedStatement) cnn.createConnection().prepareStatement(sql);
            pst.setString(1, this.manen);
            pst.executeQuery();
            
         }catch(Exception e)
         {
            JOptionPane.showMessageDialog(null, e);
         }
   }
    
}
